package cn.dong.statistics.controller;


import cn.dong.api.domain.CommonResult;
import cn.dong.statistics.domain.Daily;
import cn.dong.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-03-02
 */
@RestController
@RequestMapping("/statistics/daily")
public class DailyController {

    @Autowired
    private DailyService dailyService;

    /**
     * 手动统计日数据
     * @param day
     * @return
     */
    @PostMapping("/updateStatisticsInfo/{day}")
    public CommonResult updateStatisticsInfo(@PathVariable("day") String day){
        // 判断一下今天是否已经统计了数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        int count = dailyService.count(wrapper);
        if (count > 0) {
            // 删除之前保存的统计
            dailyService.remove(wrapper);
        }
        dailyService.updateStatisticsInfo(day);
        return CommonResult.ok();
    }

    /**
     * 获取统计信息
     * @return
     */
    @GetMapping("/getInfo")
    public CommonResult getInfo(String begin,String end){
        Map<String, Map<String, Object>> map = dailyService.getInfo(begin, end);
        return CommonResult.ok().data(map);
    }


}

